﻿using System;
using System.Collections.Generic;
using VirtuosoDal.Config;
using VirtuosoDal.Entities;
using VirtuosoDal.Repositories.RdfRepositories.Interfaces.Events;
using IEventPropertyDescriptionManager = Infrastructure.Events.Interfaces.IEventPropertyDescriptionManager;

namespace Infrastructure.Events
{
   //ToDo: Register as Singleton
   public class EventPropertyDescriptionManager : IEventPropertyDescriptionManager
   {
      private IEventsOntologyRepository _eventsOntologyRepository;

      private static Dictionary<string, Dictionary<string, EventPropertyDescription>> _eventPropertiesDescriptionsCache = new Dictionary<string, Dictionary<string, EventPropertyDescription>>();

      public EventPropertyDescriptionManager(IEventsOntologyRepository eventsOntologyRepository)
      {
         _eventsOntologyRepository = eventsOntologyRepository;
      }

      public EventPropertyDescription GetEventPropertyDescription(string eventPropertyUri, string language)
      {
         //First call for this language
         if (!_eventPropertiesDescriptionsCache.ContainsKey(language))
         {
            _eventPropertiesDescriptionsCache[language] =
               _eventsOntologyRepository.GetAllEventPropertyDescriptions(Namespaces.EventsOntologyNamespace, language);
         }

         Dictionary<string, EventPropertyDescription> eventPropertyDescriptions = _eventPropertiesDescriptionsCache[language];

         EventPropertyDescription eventPropertyDescription = null;
         if (!eventPropertyDescriptions.TryGetValue(eventPropertyUri, out eventPropertyDescription))
            throw new Exception(String.Format("Can't find eventPropertyDescription with Uri={0}. Probably ontology is out-of-date", eventPropertyUri));

         return eventPropertyDescription;
      }
   }
}


